package com.tanhua.server.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.*;
import com.tanhua.model.domain.Announcement;
import com.tanhua.model.domain.User;
import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.*;
import com.tanhua.server.exception.BusinessException;
import com.tanhua.server.interceptor.UserHolder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MessagesService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @DubboReference
    private CommentApi commentApi;

    @DubboReference
    private AnnouncementApi announcementApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    //根据环信id查询用户详情
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        //根据环信id查询用户
        User user = userApi.findByHuanxin(huanxinId);
        //根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        //拼接返回vo对象
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友关系
    public void contacts(Long friendId) {
        //将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(), Constants.HX_USER_PREFIX + friendId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.error());
        }
        //如果注册成功，记录好友关系到mongodb
        friendApi.save(UserHolder.getUserId(),friendId);
    }

    //分页查询联系人列表
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //调用API查询当前用户的好友数据 -- List<Friend>
        List<Friend> list = friendApi.findByUserId(UserHolder.getUserId(),page,pagesize);
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //提取数据列表中的好友id
        List<Long> userIds = CollUtil.getFieldValues(list, "friendId", Long.class);
        //设置条件
        UserInfo info = new UserInfo();
        info.setNickname(keyword);
        //调用UserInfoAPI查询好友的用户详情
        Map<Long, UserInfo> userInfoMap = userInfoApi.findByIds(userIds,info);
        //构造VO对象
        List<ContactVo> vos = new ArrayList<>();
        for (Friend friend : list) {
            UserInfo userInfo = userInfoMap.get(friend.getFriendId());
            ContactVo vo = ContactVo.init(userInfo);
            vos.add(vo);
        }
        return new PageResult(page,pagesize,0,vos);
    }

    //分页查询查询点赞列表
    public PageResult likes(Integer page, Integer pagesize) {
        //调用API查询当前用户的被点赞数据 -- List<Comment>
        List<Comment> list = commentApi.findByUserId(UserHolder.getUserId(),CommentType.LIKE,page,pagesize);
        //判断list集合是否存在
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //提取数据列表中的评论人id
        List<Long> userId = CollUtil.getFieldValues(list, "userId", Long.class);
        //调用UserInfoAPI查询好友的用户详情
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userId, null);
        //构造VO对象
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = infoMap.get(comment.getUserId());
            if (userInfo != null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0,vos);
    }

    //分页查询评论列表
    public PageResult comments(Integer page, Integer pagesize) {
        //调用API查询当前用户的被评论数据 -- List<Comment>
        List<Comment> list = commentApi.findByUserId(UserHolder.getUserId(), CommentType.COMMENT, page, pagesize);
        //判断list集合是否存在
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //获取评论人id
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //查询出评论人的详细信息
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userIds, null);
        //构造VO对象
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = infoMap.get(comment.getUserId());
            if (userInfo != null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0,vos);
    }

    //分页查询喜欢列表
    public PageResult loves(Integer page, Integer pagesize) {
        //调用API查询当前用户的被评论数据 -- List<Comment>
        List<Comment> list = commentApi.findByUserId(UserHolder.getUserId(), CommentType.LOVE, page, pagesize);
        //判断list集合是否存在
        if (CollUtil.isEmpty(list)){
            return new PageResult();
        }
        //获取喜欢人id
        List<Long> userIds = CollUtil.getFieldValues(list, "userId", Long.class);
        //查询出喜欢人的详细信息
        Map<Long, UserInfo> infoMap = userInfoApi.findByIds(userIds, null);
        //构建vo对象
        List<CommentVo> vos = new ArrayList<>();
        for (Comment comment : list) {
            UserInfo userInfo = infoMap.get(comment.getUserId());
            if (userInfo != null){
                CommentVo vo = CommentVo.init(userInfo, comment);
                vos.add(vo);
            }
        }
        return new PageResult(page,pagesize,0,vos);
    }

    //查询公告信息
    public PageResult announcements(Integer page, Integer pagesize) throws ParseException {
        Page<Announcement> p = announcementApi.query(page,pagesize);
        List<Announcement> list = p.getRecords();
        List<AnnouncementVo> vos = new ArrayList<>();
        for (Announcement announcement : list) {
            AnnouncementVo vo = AnnouncementVo.init(announcement);
            vos.add(vo);
        }
        PageResult pr = new PageResult(page,pagesize,0,vos);
        return pr;
    }
}
